package com.android.gtravelling;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.android.dto.ImagesDTO;
import com.android.dto.PlacesDTO;
import com.android.dto.ProvincesDTO;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaceDetail extends Activity {
	private ImageView imgView;
	TextView txtName;
	TextView txtContent;
	int expand=0;
	PlacesDTO place = new PlacesDTO();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.province_info);
		imgView = (ImageView) findViewById(R.id.imgProvince);
		txtName = (TextView) findViewById(R.id.province_name);
		txtContent = (TextView) findViewById(R.id.txtContent);
		txtContent.setLines(5);
		
		Button btnShowMap=(Button)findViewById(R.id.btnShowMap);
		OnClickListener btnShowMapListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*final Intent intent = new Intent(Intent.ACTION_VIEW,
				          *//** Using the web based turn by turn directions url. *//*
				          Uri.parse(
				                   "http://maps.google.com/maps?" +
				                   "saddr=43.0054446,-87.9678884" +
				                   "&daddr=42.9257104,-88.0508355"));
				intent.setClassName(
		                 "com.android.gtravelling",
		                 "com.android.gtravelling.ShowMapOfPlace");
		          startActivity(intent);*/
				// TODO Auto-generated method stub
				Intent i = new Intent (v.getContext(),GoogleMapProjectActivity.class);//
				i.putExtra("searchText", "du lich "+String.valueOf(place.getName()+", Viet Nam"));
				i.putExtra(Intent.EXTRA_TEXT, "1");
        		startActivity(i);
			}
		};
		btnShowMap.setOnClickListener(btnShowMapListener);
		//================================================== direction
		Button btnDirection=(Button)findViewById(R.id.btnDirection);
		OnClickListener btnDirectionListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent (v.getContext(),GoogleMapProjectActivity.class);//
				i.putExtra("searchText", String.valueOf(place.getAddress()+", Viet Nam"));
				i.putExtra(Intent.EXTRA_TEXT, "1");
        		startActivity(i);
			}
		};
		btnDirection.setOnClickListener(btnDirectionListener);
		//===================================================
		Button btnMore = (Button) findViewById(R.id.btnDetails);
		OnClickListener btnMoreListen=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//txtContent.setLines(20);
				if(expand==0){
					int height_in_pixels = txtContent.getLineCount() * txtContent.getLineHeight(); //approx height text
					txtContent.setHeight(height_in_pixels);
					Button btn=(Button)v;
					btn.setText("<<<");
					expand=1;
				}else{
					txtContent.setLines(5);
					Button btn=(Button)v;
					btn.setText("");
					expand=0;
				}
				
			}
		};
		btnMore.setOnClickListener(btnMoreListen);
		
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			// Get data via the key
			String value1 = extras.getString(Intent.EXTRA_TEXT);
			System.out.println(value1);
			if (value1 != null) {
				int pID = Integer.valueOf(value1);
				WebserviceCaller caller = new WebserviceCaller();
				
				place = caller.selectOnePlace(pID);
				/*ArrayList<ImagesDTO> listImg = new ArrayList<ImagesDTO>();
				listImg = caller.selectAllImageOfPlace(pID);

				System.out.println(place.getName());

				txtName.setText(place.getName());
				txtContent.setText(place.getDescription());

				String imgURL = "";
				if (listImg != null) {
					imgURL += listImg.get(0).getUrl();
				}
				System.out.println(imgURL);
				if (imgURL == "") {*/
					imgView.setImageResource(R.drawable.cui_bap);
				/*} else {
					InputStream is = null;
					try {
						is = fetch(imgURL);
					} catch (Exception e) {

					}

					Drawable drawable = Drawable.createFromStream(is, "src");
					imgView.setImageDrawable(drawable);
				}*/
			}
		}
	}

	private InputStream fetch(String urlString) throws MalformedURLException,
			IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlString);
		HttpResponse response = httpClient.execute(request);
		return response.getEntity().getContent();
	}
}
